﻿@inherits RadzenComponentWithChildren
@inject NavigationManager UriHelper

@if (Visible)
{
    <ul @ref="@Element" style="@Style" @attributes="Attributes" class="@GetCssClass()" id="@GetId()">
        <CascadingValue Value=this>
            @ChildContent
        </CascadingValue>
    </ul>
}

@code {
    [Parameter]
    public EventCallback<MenuItemEventArgs> Click { get; set; }

    List<RadzenPanelMenuItem> items = new List<RadzenPanelMenuItem>();

    public void AddItem(RadzenPanelMenuItem item)
    {
        if (items.IndexOf(item) == -1)
        {
            items.Add(item);
            SelectItem(item);
            StateHasChanged();
        }
    }

    protected override void OnInitialized()
    {
        UriHelper.LocationChanged += UriHelper_OnLocationChanged;
    }

    private void UriHelper_OnLocationChanged(object sender, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e)
    {
        foreach (var item in items)
        {
            SelectItem(item);
        }
    }

    public override void Dispose()
    {
        base.Dispose();
        UriHelper.LocationChanged -= UriHelper_OnLocationChanged;
    }

    bool ShouldMatch(string url)
    {
        if (string.IsNullOrEmpty(url))
        {
            return false;
        }

        var currentAbsoluteUrl = UriHelper.ToAbsoluteUri(UriHelper.Uri).AbsoluteUri;
        var absoluteUrl = UriHelper.ToAbsoluteUri(url).AbsoluteUri;

        return string.Equals(currentAbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase);
    }

    void SelectItem(RadzenPanelMenuItem item)
    {
        var selected = ShouldMatch(item.Path);
        item.Select(selected);
    }

    protected override string GetComponentCssClass()
    {
        return "rz-panel-menu";
    }
}